//
//  CollectionViewController.m
//  National flag
//
//  Created by Justsoso on 16/3/9.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "CollectionViewController.h"
#import "FlagModel.h"
#import "CollectionViewCell.h"
#import "SectionHeader.h"

@interface CollectionViewController ()

@property (nonatomic,strong) NSDictionary *flagsArrDict;
@property (nonatomic,strong) NSArray *continentNameArrs;
@end

@implementation CollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (NSArray *)continentNameArrs
{
    if (_continentNameArrs == nil)
    {
        _continentNameArrs = @[@"African Flags",@"Asian Flags",@"Australasian Flags",@"European Flags",@"North American Flags",@"South American Flags"];
    }
    return _continentNameArrs;
}

- (NSDictionary *)flagsArrDict
{
    if (_flagsArrDict == nil)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"flag" ofType:@"plist"];
        NSArray *arr = [NSArray arrayWithContentsOfFile:path];
        
        int i = 0;
        NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
        for (NSArray *tmpArr in arr)
        {
            NSMutableArray *mArr1 = [NSMutableArray array];
            for (NSDictionary *dict in tmpArr)
            {
                FlagModel *f = [FlagModel flagWithDict:dict];
                [mArr1 addObject:f];
            }
            NSString *continentName = self.continentNameArrs[i++];
            mDict[continentName] = mArr1;
        }
        _flagsArrDict = mDict;
    }
    return _flagsArrDict;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    [self.collectionView registerClass:[SectionHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
}





#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 6;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *arr2 = self.flagsArrDict[self.continentNameArrs[section]];
    return arr2.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // Configure the cell
    NSString *continentName = self.continentNameArrs[indexPath.section];
    NSArray *arr = self.flagsArrDict[continentName];
    FlagModel *flag = arr[indexPath.row];
    
    cell.flag = flag;
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        SectionHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
        
        NSString *continentName = self.continentNameArrs[indexPath.section];
        header.label.text = continentName;
        
        return header;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *continentName = self.continentNameArrs[indexPath.section];
    NSArray *flagsArr = self.flagsArrDict[continentName];
    FlagModel *flag = flagsArr[indexPath.row];
    
    [self.delegate changeFlag:flag];
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end




















